#!/bin/sh
#################################
# 2 Ls => +  6 dB
# 3 Ls => + 10 dB
# 6 Ls => + 16 dB
#################################
# 115 dB mit 2 Ls => 109 dB
# 115 dB mit 3 Ls => 105 dB
# 115 dB mit 6 Ls =>  99 dB
#################################
# http://www.epicenter.de/informationen/lautsprecher/arten-von-boxen-%11-bandpass-box/
#################################
BASE=`basename $0 .sh`
TMP=/tmp/$BASE.$$
rm -f $TMP*

if [ -f "$1" ] ; then
    IN=$1
else
    IN=in.in
fi
if [ ! -f "$IN" ] ; then
    echo "Keine Eingabe"
fi
OUT=`basename $IN .in`.$BASE.out

export LANG=en_US
cat <<EOF > $TMP.awk
BEGIN{
    OFMT="%10.5f"
    dlist[1]=40
    dname[1]="1x40mm"
    dlist[2]=50
    dname[2]="1x50mm"
    dlist[3]=70
    dname[3]="1x70mm"
    dlist[4]=100
    dname[4]="1x100mm"
    dlist[5]=141
    dname[5]="2x100mm"
    dlist[6]=173
    dname[6]="3x100mm"
    dlist[7]=200
    dname[7]="4x100mm"
    dlist[8]=224
    dname[8]="5x100mm"
    dlist[9]=245
    dname[9]="6x100mm"
    dimax=9    

    lmin=20
    lmax=800

    qbp[0.7,-8] = 0.4507; fl[0.7,-8] = 0.2167; fh[0.7,-8] = 0.9373; 
    qbp[0.7,-7] = 0.4774; fl[0.7,-7] = 0.2378; fh[0.7,-7] = 0.9584; 
    qbp[0.7,-6] = 0.5057; fl[0.7,-6] = 0.2606; fh[0.7,-6] = 0.9812; 
    qbp[0.7,-5] = 0.5356; fl[0.7,-5] = 0.2852; fh[0.7,-5] = 1.0058; 
    qbp[0.7,-4] = 0.5674; fl[0.7,-4] = 0.3118; fh[0.7,-4] = 1.0324; 
    qbp[0.7,-3] = 0.6010; fl[0.7,-3] = 0.3404; fh[0.7,-3] = 1.0610; 
    qbp[0.7,-2] = 0.6366; fl[0.7,-2] = 0.3712; fh[0.7,-2] = 1.0918; 
    qbp[0.7,-1] = 0.6743; fl[0.7,-1] = 0.4043; fh[0.7,-1] = 1.1248; 
    qbp[0.7, 0] = 0.7143; fl[0.7, 0] = 0.4397; fh[0.7, 0] = 1.1603; 
    qbp[0.7, 1] = 0.7566; fl[0.7, 1] = 0.4777; fh[0.7, 1] = 1.1983; 
    qbp[0.7, 2] = 0.8014; fl[0.7, 2] = 0.5184; fh[0.7, 2] = 1.2390; 
    qbp[0.7, 3] = 0.8489; fl[0.7, 3] = 0.5619; fh[0.7, 3] = 1.2825; 
    qbp[0.7, 4] = 0.8772; fl[0.7, 4] = 0.6084; fh[0.7, 4] = 1.3290; 
    qbp[0.7, 5] = 0.9525; fl[0.7, 5] = 0.6581; fh[0.7, 5] = 1.3787; 
    qbp[0.7, 6] = 1.0090; fl[0.7, 6] = 0.7111; fh[0.7, 6] = 1.4317; 
    qbp[0.7, 7] = 1.0687; fl[0.7, 7] = 0.7675; fh[0.7, 7] = 1.4881; 
    qbp[0.7, 8] = 1.1321; fl[0.7, 8] = 0.8277; fh[0.7, 8] = 1.5483; 

    qbp[0.6,-8] = 0.5258; fl[0.6,-8] = 0.2167; fh[0.6,-8] = 0.9373; 
    qbp[0.6,-7] = 0.5570; fl[0.6,-7] = 0.2378; fh[0.6,-7] = 0.9584; 
    qbp[0.6,-6] = 0.5900; fl[0.6,-6] = 0.2606; fh[0.6,-6] = 0.9812; 
    qbp[0.6,-5] = 0.6249; fl[0.6,-5] = 0.2852; fh[0.6,-5] = 1.0058; 
    qbp[0.6,-4] = 0.6619; fl[0.6,-4] = 0.3118; fh[0.6,-4] = 1.0324; 
    qbp[0.6,-3] = 0.7012; fl[0.6,-3] = 0.3404; fh[0.6,-3] = 1.0610; 
    qbp[0.6,-2] = 0.7427; fl[0.6,-2] = 0.3712; fh[0.6,-2] = 1.0918; 
    qbp[0.6,-1] = 0.7867; fl[0.6,-1] = 0.4043; fh[0.6,-1] = 1.1248; 
    qbp[0.6, 0] = 0.8333; fl[0.6, 0] = 0.4397; fh[0.6, 0] = 1.1603; 
    qbp[0.6, 1] = 0.8827; fl[0.6, 1] = 0.4777; fh[0.6, 1] = 1.1983; 
    qbp[0.6, 2] = 0.9350; fl[0.6, 2] = 0.5184; fh[0.6, 2] = 1.2390; 
    qbp[0.6, 3] = 0.8489; fl[0.6, 3] = 0.5619; fh[0.6, 3] = 1.2825; 
    qbp[0.6, 4] = 0.8772; fl[0.6, 4] = 0.6084; fh[0.6, 4] = 1.3290; 
    qbp[0.6, 5] = 0.9525; fl[0.6, 5] = 0.6581; fh[0.6, 5] = 1.3787; 
    qbp[0.6, 6] = 1.0090; fl[0.6, 6] = 0.6111; fh[0.6, 6] = 1.4317; 
    qbp[0.6, 7] = 1.0687; fl[0.6, 7] = 0.6675; fh[0.6, 7] = 1.4881; 
    qbp[0.6, 8] = 1.1321; fl[0.6, 8] = 0.8277; fh[0.6, 8] = 1.5483; 

    qbp[0.5,-8] = 0.6310; fl[0.5,-8] = 0.2600; fh[0.5,-8] = 1.5312; 
    qbp[0.5,-7] = 0.6683; fl[0.5,-7] = 0.2867; fh[0.5,-7] = 1.5579; 
    qbp[0.5,-6] = 0.7079; fl[0.5,-6] = 0.3158; fh[0.5,-6] = 1.5870; 
    qbp[0.5,-5] = 0.7499; fl[0.5,-5] = 0.3474; fh[0.5,-5] = 1.6186; 
    qbp[0.5,-4] = 0.7943; fl[0.5,-4] = 0.3817; fh[0.5,-4] = 1.6528; 
    qbp[0.5,-3] = 0.8414; fl[0.5,-3] = 0.4189; fh[0.5,-3] = 1.6900; 
    qbp[0.5,-2] = 0.8913; fl[0.5,-2] = 0.4591; fh[0.5,-2] = 1.7302; 
    qbp[0.5,-1] = 0.9441; fl[0.5,-1] = 0.5025; fh[0.5,-1] = 1.7736; 
    qbp[0.5, 0] = 1.0000; fl[0.5, 0] = 0.5493; fh[0.5, 0] = 1.8204; 
    qbp[0.5, 1] = 1.0593; fl[0.5, 1] = 0.5997; fh[0.5, 1] = 1.8709; 
    qbp[0.5, 2] = 1.1220; fl[0.5, 2] = 0.6540; fh[0.5, 2] = 1.9251; 
    qbp[0.5, 3] = 1.1885; fl[0.5, 3] = 0.7122; fh[0.5, 3] = 1.9833; 
    qbp[0.5, 4] = 1.2589; fl[0.5, 4] = 0.7747; fh[0.5, 4] = 2.0458; 
    qbp[0.5, 5] = 1.3335; fl[0.5, 5] = 0.8417; fh[0.5, 5] = 2.1128; 
    qbp[0.5, 6] = 1.4125; fl[0.5, 6] = 0.9134; fh[0.5, 6] = 2.1845; 
    qbp[0.5, 7] = 1.4962; fl[0.5, 7] = 0.9901; fh[0.5, 7] = 2.2612; 
    qbp[0.5, 8] = 1.5849; fl[0.5, 8] = 1.0720; fh[0.5, 8] = 2.3431; 
}
{
    name=\$1
    vas=\$2
    qts=\$3
    qes=\$4
    qms=\$5
    fs=\$6
    mms=\$7
    cms=\$8
    nvc=\$9
    re1=\$10
    le1=\$11
    sd=\$12
    xmax=\$13
    spl=\$14
    pmax=\$15
    if( nvc == 1 ) {
        re = re1;
        dwunsch = sqrt( sd / 5 ) * 10;
        for(di=1;di<=dimax;di++) {
           if( dwunsch < dlist[di] )
               break;
        }
        d  = dlist[di];
        
        printf("-------------\n%s\n",name);
        printf("           %5.1fdB          %4.0fW     %8s\n", spl, pmax, dname[di] );
        printf("   fu     fb      fo      SplMech  Vb1     Vb2       d         l        C");
        printf("\n");
        r=0.2;
        
        qes1 = qes * ( re + r ) / re;
        qts1=1/((1/qms)+(1/qes1))

        for( S = 0.7; S >= 0.5; S -= 0.1 ) {
            for( empf = -8; empf <= 8; empf++ ) {
                 fu = fl[S,empf] * fs / qts1;
                fo = fh[S,empf] * fs / qts1;
                 vf = (( 2 * S * qts1 ) ^ 2 ) * vas;
                 vr = vas / ( ((qbp[S,empf]/qts1)^2) - 1  );
                 fb = qbp[S,empf] * fs / qts1;
                 
                for(di=1;di<=dimax;di++) {
                   if( dwunsch < dlist[di] )
                       break;
                }
                d  = dlist[di];
                l  = 23562 * d * d / ((fb ^ 2) * vf );
                dkorr=" "
                if( l > lmax && di > 1 ) {
                    di--;
                    d = dlist[di];
                    l  = 23562 * d * d / ((fb ^ 2) * vf );
                    dkorr="-"
                    if( l > lmax ) {
                        di--;
                        d = dlist[di];
                        l  = 23562 * d * d / ((fb ^ 2) * vf );
                        dkorr="*"
                    }
                }
                while( l < lmin && di < dimax ) {
                    di++;
                    d = dlist[di];
                    l  = 23562 * d * d / ((fb ^ 2) * vf );
                    dkorr="+"
                }
        
                vd = sd * 2 * xmax / 10;
                spl2 = 20 * log( 0.37 * fu * fu * vd ) / log(10);
                
                if(( vr >= 1 )&&( vf >= 1 )&&( fu < 85 ))
                    printf("%5.1fHz %3.0fHz %4.0fHz %6.1fdB %4.0fl +%4.0fl  %1s %-8s %4.0fmm\n",
                             fu,fb,fo,spl2,vr,vf,dkorr,dname[di],l);
            }
        }
    }
}
END{
}
EOF

grep -v \# $IN|grep .|awk -F, -f $TMP.awk 2>&1 |tee $OUT
if [ ! -s "$OUT" ] ; then
    rm $OUT
fi
rm $TMP.* 
